﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.util;
using System.Windows;
using System.Windows.Input;
using System.Configuration;
using System.Windows.Controls;
using Newtonsoft.Json.Linq;

namespace RPAVIEW.views.loginAndRegist {
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow:Window {
        public LoginWindow() {
            InitializeComponent();
            this.DataContext = new UserInfo();
        }

        private void DragMove(object sender, MouseButtonEventArgs e) {
            this.DragMove();
        }

        private void CloseWindow(object sender, RoutedEventArgs e) {
            this.Close();
            Application.Current.Shutdown();
        }

        private void Login(object sender, RoutedEventArgs e) {
            UserInfo ui = (UserInfo)this.DataContext;
            ui.password = this.pw.Password;
            HttpUtil httpUtil = HttpUtil.getInstance(SystemUtil.GetAPPConfig("ControlBaseIp") + SystemUtil.GetAPPConfig("UserUri"));
            HttpResponse<object> response = httpUtil.SendPost<object>("/login", ui);
            if(response.Code == 500) { 
                MessageBox.Show(response.Msg);
                return;
            } else { 
                UserInfo user = (UserInfo)(response.Data as JObject).ToObject(typeof(UserInfo));
                (Application.Current as App).currentUser = user;
                MainWindow wnd = new MainWindow();
                wnd.Show();
                this.Close();
            }
        }

        private void GoRegWin(object sender, RoutedEventArgs e) {
            RegisterWindow regW = new RegisterWindow();
            regW.Show();
            this.Close();
        }
    }
}
